package com.chuangjiangx.member.score.ddd.domain.repository;



import com.chuangjiangx.dddbase.Repository;
import com.chuangjiangx.domain.shared.model.Enable;
import com.chuangjiangx.member.score.dao.mapper.InMbrScoreGrandTotalRuleMapper;
import com.chuangjiangx.member.score.dao.model.InMbrScoreGrandTotalRule;
import com.chuangjiangx.member.score.dao.model.InMbrScoreGrandTotalRuleExample;
import com.chuangjiangx.member.basic.ddd.domain.model.MbrScoreGrandTotalRuleId;
import com.chuangjiangx.member.basic.ddd.domain.model.MbrScoreGrandTotalRuleScope;
import com.chuangjiangx.member.basic.ddd.domain.model.MbrScoreGrandTotalRuleType;
import com.chuangjiangx.member.basic.ddd.domain.model.MerchantId;
import com.chuangjiangx.member.score.ddd.domain.model.MbrScoreGrandTotalRule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;


/**
 * 积分累计规则持久类
 */
@Component
public class MbrScoreGrandTotalRuleRepository implements Repository<MbrScoreGrandTotalRule, MbrScoreGrandTotalRuleId> {

    @Autowired
    private InMbrScoreGrandTotalRuleMapper inMbrScoreGrandTotalRuleMapper;

    @Override
    public MbrScoreGrandTotalRule fromId(MbrScoreGrandTotalRuleId mbrScoreGrandTotalRuleId) {
        InMbrScoreGrandTotalRule in = inMbrScoreGrandTotalRuleMapper.selectByPrimaryKey(mbrScoreGrandTotalRuleId.getId());
        return null == in ? null : wrap(in);
    }

    @Override
    public void update(MbrScoreGrandTotalRule mbrScoreGrandTotalRule) {
        InMbrScoreGrandTotalRule in = unbox(mbrScoreGrandTotalRule);
        inMbrScoreGrandTotalRuleMapper.updateByPrimaryKeySelective(in);
    }


    @Override
    public void save(MbrScoreGrandTotalRule mbrScoreGrandTotalRule) {
        InMbrScoreGrandTotalRule in = unbox(mbrScoreGrandTotalRule);
        inMbrScoreGrandTotalRuleMapper.insertSelective(in);
        mbrScoreGrandTotalRule.setId(new MbrScoreGrandTotalRuleId(in.getId()));
    }

    public MbrScoreGrandTotalRule fromMerchantId(MerchantId merchantId) {
        InMbrScoreGrandTotalRuleExample example = new InMbrScoreGrandTotalRuleExample();
        example.createCriteria()
                .andMerchantIdEqualTo(merchantId.getId());
        List<InMbrScoreGrandTotalRule> list = inMbrScoreGrandTotalRuleMapper.selectByExample(example);
        return list.isEmpty() ? null : wrap(list.get(0));
    }

//    public MbrScoreGrandTotalRule getRuleByMerchantIdAndId(MerchantId merchantId, MbrScoreGrandTotalRuleId id) {
//        InMbrScoreGrandTotalRuleExample example = new InMbrScoreGrandTotalRuleExample();
//        example.createCriteria().andMerchantIdEqualTo(merchantId.getId()).andIdEqualTo(id.getId())
//                .andEnableEqualTo(Enable.ENABLE.value);
//        List<InMbrScoreGrandTotalRule> list = inMbrScoreGrandTotalRuleMapper.selectByExample(example);
//        if (list != null && list.size() > 0) {
//            return wrap(list.get(0));
//        }
//        return null;
//    }
//
//    public List<MbrScoreGrandTotalRule> getRuleByMerchantIdAndName(MerchantId merchantId, String name) {
//        InMbrScoreGrandTotalRuleExample example = new InMbrScoreGrandTotalRuleExample();
//        example.createCriteria().andMerchantIdEqualTo(merchantId.getId()).andNameEqualTo(name)
//                .andEnableEqualTo(Enable.ENABLE.value);
//        return transformList(inMbrScoreGrandTotalRuleMapper.selectByExample(example));
//    }
//
//    private List<MbrScoreGrandTotalRule> transformList(List<InMbrScoreGrandTotalRule> list) {
//
//        List<MbrScoreGrandTotalRule> ruleList = new ArrayList<>();
//        list.forEach(inMbrScoreGrandTotalRule -> {
//            ruleList.add(wrap(inMbrScoreGrandTotalRule));
//        });
//        return ruleList;
//    }

    private MbrScoreGrandTotalRule wrap(InMbrScoreGrandTotalRule in) {
        MbrScoreGrandTotalRule m = new MbrScoreGrandTotalRule(
                new MbrScoreGrandTotalRuleId(in.getId()),
                new MerchantId(in.getMerchantId()),
                in.getName(),
                MbrScoreGrandTotalRuleType.getPlusScoreRuleType(in.getType()),
                MbrScoreGrandTotalRuleScope.getPlusScoreRuleScope(in.getScope()),
                in.getAmount(),
                in.getScore(),
                Enable.getEnable(in.getEnable()),
                in.getCreateTime(),
                in.getUpdateTime());
        return m;
    }

    private InMbrScoreGrandTotalRule unbox(MbrScoreGrandTotalRule m){
        InMbrScoreGrandTotalRule in = new InMbrScoreGrandTotalRule();
        in.setId(null == m.getId() ? null : m.getId().getId());
        in.setAmount(m.getAmount());
        in.setEnable(null == m.getEnable() ? null : m.getEnable().value);
        in.setMerchantId(null == m.getMerchantId() ? null : m.getMerchantId().getId());
        in.setName(m.getName());
        in.setScope(null == m.getScope() ? null : m.getScope().value);
        in.setScore(m.getScore());
        in.setType(null == m.getType() ? null : m.getType().value);
        in.setCreateTime(m.getTimestamp().getCreateTime());
        in.setUpdateTime(m.getTimestamp().getUpdateTime());
        return in;
    }
}
